<?php


namespace Xy\Rbac\Requests;


use Illuminate\Foundation\Http\FormRequest;

abstract class BaseFormRequest extends FormRequest {
    
    /**
     * 使用'.'号分割,e.g, $_localeAttributesFile = 'admin.rule';
     * @var string
     */
    protected $_localeAttributesFile;
    
    /**
     * 根据路由和请求方法定义验证规则
     * $_requestRules = [
     *    'api/users/{id}' => [
     *         'PUT'  => [
     *               'name'   => 'required|string|max:255',
     *               'credit' => 'required|integer|between:0,100',
     *          ],
     *         'DELETE' => [
     *               'reason' => 'required|string|max:255',
     *          ],
     *    ],
     *    'api/users' => [
     *         'POST' => [
     *               'name'  => 'required|string|max:255',
     *               'email' => 'required|email',
     *          ],
     *    ],
     * ];
     * @var array
     */
    protected $_requestRules = [];
    
    /**
     * @return array
     */
    public function rules() {
        $uri = request()->route()->uri();//e.g, api/users/{id}
        
        return $this->_requestRules[$uri][$this->method()] ?? [];
    }
    
    /**
     * @return array
     */
    public function messages() {
        return $this->_attributesAndMessageInfo['messages'] ?? [];
    }
    
    /**
     * @return bool
     */
    public function authorize() {
        return true;
    }
    
    
    const ATTRIBUTE_FILE_SEPARATOR = '.';
    const ATTRIBUTE_FILE_SUFFIX    = '.php';
    const ATTRIBUTE_FILE_BASE_DIR  = 'form_request';
    
    /**
     * @return array
     */
    public function attributes() {
        return $this->_attributesAndMessageInfo['attributes'] ?? [];
    }
    
    /**
     * 用于快速填充不同场景下model实例可更新或插入的安全字段
     * e.g ,
     * $userModel->fillable($userRequestForm->getRuleKeys())
     *           ->file($data)->save();
     * @return array
     */
    public function getRuleKeys() {
        return array_keys($this->rules());
    }
    
    /**
     * 初始化执行操作,可以在此定义一些复杂的规则内容
     */
    protected function _formRequestInit() {
    
    }
    
    protected $_attributesAndMessageInfo;
    
    public function __construct(array $query = array(), array $request = array(), array $attributes = array(), array $cookies = array(), array $files = array(), array $server = array(), $content = null) {
        $this->_loadAttributesAndMessageInfo();
        parent::__construct($query, $request, $attributes, $cookies, $files, $server, $content);
        $this->_formRequestInit();
    }
    
    /**
     * 加载字段别名和自定义验证信息提示
     */
    protected function _loadAttributesAndMessageInfo() {
        $attributesAndMessageInfo = [];
        if ($this->_localeAttributesFile) {
            $localeAttributesFile = implode(DIRECTORY_SEPARATOR, [
                    resource_path('lang'),
                    app()->getLocale(),
                    static::ATTRIBUTE_FILE_BASE_DIR,
                    str_replace(static::ATTRIBUTE_FILE_SEPARATOR, DIRECTORY_SEPARATOR, $this->_localeAttributesFile),
                ]) . static::ATTRIBUTE_FILE_SUFFIX;
            
            if (file_exists($localeAttributesFile)) {
                $attributesAndMessageInfo = include($localeAttributesFile);
            }
        }
        
        $this->_attributesAndMessageInfo = $attributesAndMessageInfo;
    }
}